package com.dreamder.view;

import java.util.ArrayList;
import java.util.List;

import com.dreamder.model.CategoriaBeanTO;
import com.dreamder.model.SonhoBeanTO;
import com.dreamder.model.UsuarioBeanTO;
import com.dreamder.service.SonhoService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NovoSonhoActivity extends Activity {

	Intent i;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_novo_sonho);

		Spinner sp = (Spinner) findViewById(R.id.sonhoSpinner);

		List<CategoriaBeanTO> lista = new ArrayList<CategoriaBeanTO>();
		CategoriaBeanTO c1 = new CategoriaBeanTO();
		c1.setId(1L);
		c1.setDescricao("Viagem");
		CategoriaBeanTO c2 = new CategoriaBeanTO();
		c2.setId(2L);
		c2.setDescricao("Passeios");

		lista.add(c1);
		lista.add(c2);

		ArrayAdapter<CategoriaBeanTO> ad = new ArrayAdapter<CategoriaBeanTO>(
				this, android.R.layout.simple_spinner_item, lista);

		sp.setAdapter(ad);

		i = new Intent(this, ListaSonhosActivity.class);

	}

	public void salvarSonho(View v) {

		SonhoBeanTO sonho = new SonhoBeanTO();

		TextView titulo = (TextView) findViewById(R.id.txtNovoSonhoTitulo);
		sonho.setTitulo(titulo.getText().toString());

		TextView resumo = (TextView) findViewById(R.id.txtNovoSonhoResumo);
		sonho.setResumo(resumo.getText().toString());

		TextView detalhe = (TextView) findViewById(R.id.txtNovoSonhoDetalhe);
		sonho.setDsDetalhada(detalhe.getText().toString());

		SharedPreferences pref = getSharedPreferences("DreamderLogin", 0);
		Long id = pref.getLong("idUsuario", 0L);

		UsuarioBeanTO u = new UsuarioBeanTO();

		u.setId(id);

		sonho.setDono(u);

		Spinner sp = (Spinner) findViewById(R.id.sonhoSpinner);
		CategoriaBeanTO c = (CategoriaBeanTO) sp.getSelectedItem();

		if (c != null) {
			sonho.setCategoria(c);
		}

		SalvarSonhoTask task = new SalvarSonhoTask();

		task.execute(sonho);

	}

	private class SalvarSonhoTask extends AsyncTask<SonhoBeanTO, Void, Boolean> {

		private ProgressDialog p;

		@Override
		protected void onPreExecute() {
			p = ProgressDialog.show(NovoSonhoActivity.this, "Aguarde...",
					"Cadastrando...");
		}

		@Override
		protected Boolean doInBackground(SonhoBeanTO... params) {

			SonhoService service = new SonhoService();
			Boolean result = service.salvarSonho(params[0]);

			return result;
		}

		@Override
		protected void onPostExecute(Boolean result) {

			p.dismiss();

			if (result) {
				Toast.makeText(NovoSonhoActivity.this,
						"Sonho cadastrado com sucesso!", Toast.LENGTH_LONG)
						.show();
				startActivity(i);

			} else {
				Toast.makeText(NovoSonhoActivity.this,
						"Ocorreu um erro, tente novamente!", Toast.LENGTH_LONG)
						.show();
			}

		}
	}
}
